package com.axend.aerosense.common.util;

import android.icu.util.TimeZone;
import android.text.TextUtils;
import android.util.Log;

import com.axend.aerosense.base.utils.SystemUtil;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {
    public static String getStringDataMin(String strDate) {
        if (TextUtils.isEmpty(strDate)) {
            return "";
        }
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
        Date tmp = strToDate(strDate);
        if (null == tmp) {
            return "";
        }
        String dateString = formatter.format(tmp);
        return dateString;
    }

    public static String getStringDateMonthShort(String strDate) {
        if (TextUtils.isEmpty(strDate)) {
            return "";
        }
        SimpleDateFormat formatter = new SimpleDateFormat("MM月dd日 HH:mm");
        Date tmp = strToDateLong(strDate);
        if (null == tmp) {
            return "";
        }
        String dateString = formatter.format(tmp);
        return dateString;
    }

    /**
     * 将日期转换 并且判断是否是今天或者今年
     * 今天显示 时间 19:30
     * 今年显示 月/日 04/24
     * 不是今年显示  月/日/年  04/23/2021
     *
     * @param strDate
     * @return
     */
    public static String getStringDateJudgeToday(String strDate) {
        if (!TextUtils.isEmpty(strDate)) {
            SimpleDateFormat sf;
            if (isNow(strToDateLong(strDate))) {//是否是当天
                sf = new SimpleDateFormat("HH:mm");
            } else {
                if(isYear(strToDateLong(strDate))){//是否是今年
                    sf = new SimpleDateFormat("MM/dd");
                }else{
                    sf = new SimpleDateFormat("MM/dd/yyyy");
                }
            }
            Date date = strToDateLong(strDate);
            String dateStr = sf.format(date);
            return dateStr;
        }
        return "";
    }

    /**
     * 判断日期是不是今天
     *
     * @param date
     * @return 是返回true，不是返回false
     */
    public static boolean isNow(Date date) {
        // 默认的年月日的格式. yyyy-MM-dd
        String patten = "yyyy-MM-dd";
        // 当前时间
        Date now = new Date();
        SimpleDateFormat sf = new SimpleDateFormat(patten);
        //获取今天的日期
        String nowDay = sf.format(now);
        //对比的时间
        String day = sf.format(date);

        return day.equals(nowDay);
    }
    /**
     * 判断日期是不是今年
     *
     * @param date
     * @return 是返回true，不是返回false
     */
    public static boolean isYear(Date date) {
        // 默认的年月日的格式. yyyy-MM-dd
        String patten = "yyyy";
        // 当前时间
        Date now = new Date();
        SimpleDateFormat sf = new SimpleDateFormat(patten);
        //获取今天的日期
        String nowDay = sf.format(now);
        //对比的时间
        String day = sf.format(date);

        return day.equals(nowDay);
    }

    public static String getStringDateMMddyyyyHHmm(String strDate) {
        if (!TextUtils.isEmpty(strDate)) {
            try{
                SimpleDateFormat sf;
                sf = new SimpleDateFormat("MM/dd/yyyy HH:mm");
                Date date = strToDateLong(strDate);
                String dateStr = sf.format(date);
                return dateStr;
            }catch (Exception e){
                return strDate;
            }
        }
        return "";
    }

    public static Date strToDateLong(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        if (null == strtodate) {
            formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm");
            strtodate = formatter.parse(strDate, pos);
        }
        return strtodate;
    }


    public static Date strToDate(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        if (null == strtodate) {
            formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm");
            strtodate = formatter.parse(strDate, pos);
        }
        return strtodate;
    }


    public static Date strYMDToDate(String strDate) {
        if (TextUtils.isEmpty(strDate)) {
            return null;
        }
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        if (null == strtodate) {
            formatter = new SimpleDateFormat("yyyy/MM/dd");
            strtodate = formatter.parse(strDate, pos);
        }
        return strtodate;
    }

    /**
     * 获取英文年月日
     *
     * @param strDate
     * @return
     */
    public static String getEnglishDateString(String strDate) {
        String dateString = "";
        String formate = getEnglishDateFormatString(strDate);
        if (!TextUtils.isEmpty(formate)) {
            String[] subStrings = formate.split("-");
            if (null != subStrings && subStrings.length == 3) {
                String tmp = subStrings[0];
                if (tmp.startsWith("0")) {
                    tmp = tmp.replace("0", "");
                }
                try {
                    int month = Integer.parseInt(tmp);
                    dateString = SystemUtil.getMonth(month);
                    dateString = dateString + " " + subStrings[1] + " " + subStrings[2];
                } catch (Exception e) {

                }
            }
        }

        return dateString;
    }

    public static String getEnglishDateFormatString(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("MM-dd-yyyy");
        Date tmp = strYMDToDate(strDate);
        if (null == tmp) {
            return "";
        }
        String dateString = formatter.format(tmp);
        return dateString;
    }

    public static String getEnglishDateFormatString2(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
        Date tmp = strYMDToDate(strDate);
        if (null == tmp) {
            return "";
        }
        String dateString = formatter.format(tmp);
        return dateString;
    }

    public static String getENVersionData(String strDate) {
        if (!SystemUtil.isChineseLanguage()) {
            return getEnglishDateString(strDate);
        }
        return strDate;
    }


}
